/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-26
*/
package com.rzico.basics.controller.member;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.MemberFavorites;
import com.rzico.basics.entity.MemberTrace;
import com.rzico.basics.entity.Product;
import com.rzico.basics.model.ProductVo;
import com.rzico.basics.service.MemberService;
import com.rzico.basics.service.MemberTraceService;
import com.rzico.basics.service.ProductService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 足迹控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-05-26
 */
@Api(description = "足迹接口")
@RestController
@RequestMapping("/member/memberTrace")
public class MemberTraceMemberController extends BaseController {

    @Autowired
    private MemberTraceService memberTraceService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ProductService productService;

    /**
     * 分页查询足迹
     *
     * @return
    */
    @ApiOperation("分页查询足迹")
    @GetMapping("/list")
    public CommResult<ProductVo> list(Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);

        Member member = memberService.getCurrent();

        if (member!=null) {
            params.put("memberId",member.getId());
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ProductVo> list = memberTraceService.selectProductVoList(params);
        PageResult<ProductVo> pageResult = new PageResult<ProductVo>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 获取足迹记录数
     *
     * @return
     */
    @ApiOperation("获取足迹记录数")
    @GetMapping("/count")
    public CommResult<Long> count() {
        Member member = memberService.getCurrent();
        Map<String,Object> params = new HashMap<>();
        params.put("memberId",member.getId());
        //获取足迹
        Integer tae = memberTraceService.selectRowCount(params);
        return CommResult.success(tae);

    }


    /**
     * 添加足迹
     *
     * @param memberTrace
     * @return
     */
    @ApiOperation("添加足迹")
    @PostMapping("/add")
    public CommResult<MemberTrace> add(@RequestBody MemberTrace memberTrace) {

        Member member = memberService.getCurrent();
        Product product = productService.findById(memberTrace.getProductId());
        Map<String,Object> params = new HashMap<>();
        params.put("enterpriseId",product.getEnterpriseId());
        params.put("memberId",member.getId());
        params.put("startDate", DateUtils.truncate(new Date(), Calendar.DATE));
        params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(new Date(),1),-1));

        Integer w = memberTraceService.selectRowCount(params);
        if (w>0) {
            CommResult.success();
        }

        memberTrace.setEnterpriseId(product.getEnterpriseId());
        memberTrace.setCreateDate(new Date());
        memberTrace.setModifyDate(new Date());
        memberTrace.setMemberId(member.getId());
        memberTrace.setProductType(0);
        //判断今天是否有记录

        int affectCount = memberTraceService.insertUseGeneratedKeys(memberTrace);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        return CommResult.success();

    }


    /**
     * 批量删除足迹
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除足迹", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除足迹,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<MemberTrace> del(@PathVariable String ids) {

        memberTraceService.deleteByIds(ids.split(","));
        return CommResult.success();
    }


}
